import uuid


# 唯一标识，uuid4通过伪随机数，生成通用唯一标识
# print(uuid.uuid4())  # 2a9256d3-54c9-4c81-aced-bfbc1824b47e


from threading import Thread
import requests

#
# def func1(num):
#     print(f"线程-{num}")
#
#     response = requests.request(method="get", url="http://192.168.124.12:8899/user/area", params={"city_id": 2})
#     print(response.json())
#
#
# for i in range(1):
#     t = Thread(target=func1, args=(i,))
#     t.start()


# insert_dict = {"id": 100, "work_order_id": "xpcs", "type_order": 1}
# table_name = "work_order"
# insert_keys = ', '.join(insert_dict.keys())
# insert_value = tuple(insert_dict.values())
# insert_place_holder = ', '.join(["%s" for i in insert_value])
#
# print(insert_keys)
# print(insert_value)
# print(insert_place_holder)
# sql_str = f"""insert into {table_name} ({insert_keys}) values ({insert_place_holder})"""
# print(sql_str)
import re

# 我们分别验证无条件，一条件，二条件
# phone_number = 15135266622
# city_id = 1
sql_str = """select id, work_order_id from flask_work_order order by id asc"""
condition_dict = {"city_id": 1, "phone_number": "15135266622"}


def select_helper(sql_str, condition_dict={}, limit_dict={}):
    # 处理sql，如果包含排序则将排序拿出，拼接条件，最后拼接排序
    order_by_str = None

    if "order by " in sql_str:
        sql_str_list = re.split(pattern=" order by ", string=sql_str)
        sql_str = sql_str_list[0]
        order_by_str = sql_str_list[1]

    # 拼接条件
    if condition_dict:
        sql_str += " where "
        for k in condition_dict:
            sql_str += str(k) + "=%s and "
        # 去除结尾and
        sql_str = re.sub(pattern=" and $", repl="", string=sql_str)
        condition_value = tuple(condition_dict.values())

    # 拼接排序
    if order_by_str:
        sql_str += " order by " + order_by_str

    # 分页字典约定固定格式 {"page_num": 1, "page_size": 10}
    if limit_dict:
        page_size = limit_dict.get("page_size")
        page_num = limit_dict.get("page_num")
        limit_str = f" limit {(page_num - 1) * page_size}, {page_size}"
        sql_str += limit_str

    print(sql_str)

print(sql_str)
# 使用order by 拆分语句
# sql_str = re.split(pattern="order by", string=sql_str)
# print(sql_str)
select_helper(sql_str, condition_dict, limit_dict={"page_size": 10, "page_num": 2})

dict1 = {"page_num": 1, "page_size": 10}
print(dict1.pop("page_num")) # 1
print(dict1)
print(1 in (None, ''))

import datetime


print(datetime.datetime(2024,6,21).strftime("%Y-%m-%d %H:%M:%S"))

print("=====")

dict1 = {"name": "xpcs"}
# dict1.pop("name1")  KeyError: 'name1'
print(dict1)
print(None == 0)

